package csc.fresher_group2.servlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.jni.User;

import csc.fresher_group2.dao.IFileDAO;
import csc.fresher_group2.dao.IFileDAOImpl;
import csc.fresher_group2.model.Account;
import csc.fresher_group2.model.FileInfo;

/**
 * Servlet implementation class FileServlet
 */
public class FileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public FileServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doActionFiles(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doActionFiles(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @author Phuc
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	protected void doActionFiles(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		Account user = (Account) session.getAttribute("account");
		String page = "files.jsp";
		// get path of user repository
		String rootPath = getServletContext().getRealPath(
				"\\users\\" + user.getUserName());
		// check if user not login -> redirect to login.jsp
		if (user == null) {
			response.sendRedirect("login.jsp");
		} else {

			IFileDAO iFileDao = new IFileDAOImpl();
			iFileDao.newFolder(getServletContext().getRealPath("\\users\\"),
					false);
			iFileDao.newFolder(rootPath, false);
			iFileDao.newFolder(rootPath + "\\trash", false);
			// get action of user
			String action = request.getParameter("action");
			String path = request.getParameter("path");
			if (path == null) {
				path = "";
			}

			if (action != null) {
				if (action.equals("files")) {

					request.setAttribute("path", path);
					System.out.println(path);
					System.out.println(rootPath + "\\" + path);
					FileInfo[] fInfos = iFileDao.getFilesExceptTrash(rootPath
							+ "\\" + path);

					request.setAttribute("fInfos", fInfos);

				}

				else if (action.equals("up")) {

					File currentFile = new File(rootPath + "\\" + path);
					FileInfo[] fInfos = null;
					if (path == null || path == "") {
						path = "";
						fInfos = iFileDao.getFilesExceptTrash(currentFile
								.getPath());

					} else {
						path = path.substring(0, path.length() - 1
								- currentFile.getName().length());

						request.setAttribute("path", path);
						System.out.println("up: " + path);

						fInfos = iFileDao.getFilesExceptTrash(currentFile
								.getParentFile().getPath());
					}
					request.setAttribute("fInfos", fInfos);

				} else if (action.equals("new")) {

					File currentFile = new File(rootPath + "\\" + path);

					IFileDAO iFileDAO = new IFileDAOImpl();
					iFileDAO.newFolder(currentFile.getPath() + "\\"
							+ "New Folder", true);

					FileInfo[] fInfos = iFileDao
							.getFilesExceptTrash(currentFile.getPath());
					request.setAttribute("fInfos", fInfos);

				} else if (action.equals("search")) {
					String txtSearch = request.getParameter("txtSearch");
					if (txtSearch != null) {

					}

				} else if (action.equals("move")) {

				} else if (action.equals("delete")) {

					String[] files = request.getParameterValues("fileList");
					IFileDAO iFileDAO = new IFileDAOImpl();

					if (files != null) {
						for (int i = 0; i < files.length; i++) {
							File temp = new File(rootPath + "\\" + path + "\\"
									+ files[i]);
							iFileDao.deleteFile(temp.getPath(), rootPath
									+ "\\trash");
						}
					}

					FileInfo[] fInfos = iFileDao.getFilesExceptTrash(rootPath
							+ "\\" + path);
					request.setAttribute("fInfos", fInfos);

				} else if (action.equals("download")) {

				} else if (action.equals("share")) {

				}
			}

			else {

				FileInfo[] fInfos = iFileDao.getFilesExceptTrash(rootPath);
				request.setAttribute("fInfos", fInfos);

			}

			RequestDispatcher rd = request.getRequestDispatcher(page);
			rd.forward(request, response);
		}
	}
}
